package cn.iurac.testsystem.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.iurac.testsystem.entity.User;
import cn.iurac.testsystem.enums.RoleEnum;
import cn.iurac.testsystem.exception.ServiceException;
import cn.iurac.testsystem.log.OperationLog;
import cn.iurac.testsystem.param.PageRequestParam;
import cn.iurac.testsystem.param.RequestDataParam;
import cn.iurac.testsystem.param.ResponseParam;
import cn.iurac.testsystem.param.request.apply.ApplyPageRequestParam;
import cn.iurac.testsystem.param.request.apply.ApplyRequestParam;
import cn.iurac.testsystem.param.response.ApplyListResponseParam;
import cn.iurac.testsystem.param.response.page.ApplyPageResponseParam;
import cn.iurac.testsystem.service.ApplyService;
import cn.iurac.testsystem.service.ClazzService;
import cn.iurac.testsystem.service.UserService;
import cn.iurac.testsystem.util.ResponseUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@Slf4j
@Api("申请controller")
@RequestMapping("/apply")
public class ApplyController {

    @Resource
    private ApplyService applyService;
    @Resource
    private UserService userService;
    @Resource
    private ClazzService clazzService;

    @RequiresRoles(value = {"student","teacher","admin","superadmin"},logical = Logical.OR)
    @PostMapping("/listForPage")
    @ApiOperation("申请管理：查询申请")
    @OperationLog("申请管理：查询申请")
    public ResponseParam listForPage(@RequestBody PageRequestParam<ApplyPageRequestParam> req) {
        ApplyPageRequestParam data = req.getData();

        User user = (User) SecurityUtils.getSubject().getPrincipal();
        if(ObjectUtil.isNull(user) || RoleEnum.TEACHER.getCode().equals(user.getRole())){
            data.setTeacherId(user.getId());
        }
        if(ObjectUtil.isNull(user) || RoleEnum.STUDENT.getCode().equals(user.getRole())){
            data.setStudentId(user.getId());
        }

        Page<ApplyPageResponseParam> page = new Page<>(req.getPage(),req.getLimit());
        page = applyService.listForPage(page, data);
        return new ResponseParam(page);
    }

    @RequiresRoles(value = {"student","superadmin"},logical = Logical.OR)
    @GetMapping("/listApply/{page}")
    @ApiOperation("申请管理：查询申请名单")
    @OperationLog("申请管理：查询申请名单")
    public ResponseParam listApply(@PathVariable Integer page, @RequestParam(value = "name", required = false) String name) {
        Page<ApplyListResponseParam> p = new Page<>(page,8);
        p = applyService.listApply(p, name);
        return new ResponseParam(p);
    }

    @RequiresRoles(value = {"student","admin","superadmin"},logical = Logical.OR)
    @PostMapping("/save")
    @ApiOperation("申请管理：添加申请")
    @OperationLog("申请管理：添加申请")
    public ResponseParam save(@Validated @RequestBody RequestDataParam<ApplyRequestParam> req) throws ServiceException {
        ApplyRequestParam data = req.getData();
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        ResponseParam responseParam = new ResponseParam();
        data.setStudentId(ObjectUtil.defaultIfNull(data.getStudentId(),user.getId()));
        boolean status = applyService.save(data);
        if (!status) {
            responseParam = ResponseUtil.getErrorResponseParam("系统错误!");
        }
        return responseParam;
    }

    @RequiresRoles(value = {"teacher","admin","superadmin"},logical = Logical.OR)
    @PostMapping("/update/{id}")
    @ApiOperation("申请管理：更新申请")
    @OperationLog("申请管理：更新申请")
    public ResponseParam update(@PathVariable("id") Long id, @Validated @RequestBody RequestDataParam<ApplyRequestParam> req) throws ServiceException {
        ApplyRequestParam data = req.getData();
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        if(ObjectUtil.isNull(user) || RoleEnum.TEACHER.getCode().equals(user.getRole())){
            if(!ObjectUtil.equal(data.getTeacherId(), user.getId())){
                return ResponseUtil.getErrorResponseParam("您没有权限！");
            }
        }
        ResponseParam responseParam = new ResponseParam();
        boolean status = applyService.update(id, data);
        if (!status) {
            responseParam = ResponseUtil.getErrorResponseParam("系统错误!");
        }
        return responseParam;
    }

    @RequiresRoles(value = {"admin","superadmin"},logical = Logical.OR)
    @PostMapping("/delete/{id}")
    @ApiOperation("申请管理：删除申请")
    @OperationLog("申请管理：删除申请")
    public ResponseParam delete(@PathVariable("id") Long id) {
        boolean status = applyService.removeById(id);
        ResponseParam responseParam = new ResponseParam();
        if (!status) {
            responseParam = ResponseUtil.getErrorResponseParam("系统错误!");
        }
        return responseParam;
    }

}
